//
//  ABUDCustomBannerAdapter.m
//  ABUDemo
//
//  Created by bytedance on 2021/10/20.
//  Copyright © 2021 bytedance. All rights reserved.
//

#import "JiGuangAdsBannerAdapter.h"
#import <JgAdsSDK/JgAdsSDK.h>
@interface JiGuangAdsBannerAdapter ()<JgAdsBannerAdViewDelegate>

@property (nonatomic, assign) NSTimeInterval lastLoadTimeInterval;

@property (nonatomic, strong) JgAdsBannerAdView *bannerView;

@property (nonatomic, assign) BOOL isBidType;

@end

@implementation JiGuangAdsBannerAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    BUMMediatedAdStatus status = BUMMediatedAdStatusNormal;
    if (self.lastLoadTimeInterval + 30.f > CACurrentMediaTime()) { // 模拟30秒，广告过期
        status.unexpired = BUMMediatedAdStatusValueDeny;
    }
    return status;
}

- (void)loadBannerAdWithSlotID:(nonnull NSString *)slotID andSize:(CGSize)adSize parameter:(nullable NSDictionary *)parameter {
    
    if (CGSizeEqualToSize(adSize, CGSizeZero)) {
        adSize = CGSizeMake(640, 100);
    }
    _bannerView = [[JgAdsBannerAdView alloc]initWithFrame:CGRectMake(0, 0, adSize.width, adSize.height) posId:slotID];
    _bannerView.delegate = self;
    _bannerView.viewController = self.bridge.viewControllerForPresentingModalView;
    [_bannerView loadRequest];
    if([parameter[BUMAdLoadingParamBiddingType] integerValue] == 1){
        self.isBidType = YES;
    }else{
        self.isBidType = NO;
    }
       if (self.isBidType) {
           NSLog(@"极光ads==========横幅竞价广告");
       }

}

-(void)didReceiveBidResult:(BUMMediaBidResult *)result{
    
    if (!self.bannerView) return;
    if (result.win) {
        NSLog(@"横幅==========竞价成功!竞赢价格：%ld",result.winnerPrice);
        [self.bannerView sendWinNotificationWithPrice:result.winnerPrice];
    } else {
        NSLog(@"横幅==========竞价失败!竞赢价格：%ld",result.winnerPrice);
        JgAdsBiddingLossReason reason ;
        if ([result.lossDescription isEqualToString:@"timeout"]) {
            reason = JgAdsBiddingLossReasonOther;
        }else if ([result.lossDescription isEqualToString:@"low price"]){
            reason = JgAdsBiddingLossReasonLowPrice;
        }else {
            reason = JgAdsBiddingLossReasonOther;
        }
        [self.bannerView sendWinFailNotificationReason:reason winnerPirce:result.winnerPrice];
       
    }
}

#pragma mark - JgAdsBannerAdViewDelegate

/**
 *  请求广告条数据成功后调用
 *  当接收服务器返回的广告数据成功后调用该函数
 */
- (void)jgadsBannerSuccessLoad:(JgAdsBannerAdView *)jgadsBannerView {
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
    NSLog(@"极光ads横幅竞价价格：%ld",(long)jgadsBannerView.bidPrice);
        if (self.isBidType && jgadsBannerView.bidPrice > 0) {
            [parames setValue:[NSString stringWithFormat:@"%ld",jgadsBannerView.bidPrice] forKey:BUMMediaAdLoadingExtECPM];
        }
    [self.bridge bannerAd:self didLoad:self.bannerView ext:parames];

}

/**
 *  请求广告条数据失败后调用
 *  当接收服务器返回的广告数据失败后调用该函数
 */
- (void)jgadsBannerViewFailedToLoadWithError:(NSError *)error {
    [self.bridge bannerAd:self didLoadFailWithError:error ext:@{}];
}

/**
 *  曝光回调
 */
- (void)jgadsBannerViewWillExpose:(JgAdsBannerAdView *)jgadsBannerView {
    [self.bridge bannerAdDidBecomeVisible:self bannerView:self.bannerView];
}

/**
 *  点击回调
 */
- (void)jgadsBannerViewClicked:(JgAdsBannerAdView *)jgadsBannerView {
    [self.bridge bannerAdDidClick:self bannerView:self.bannerView];
}

/**
 *  被用户关闭时调用
 */
- (void)jgadsBannerViewWillClose:(JgAdsBannerAdView *)jgadsBannerView {
    [self.bridge bannerAdDidClick:self bannerView:self.bannerView];
    [self.bridge bannerAd:self bannerView:self.bannerView didClosedWithDislikeWithReason:nil];
}

/**
 *  被用户关闭广告落地页调用
 */
- (void)jgadsBannerViewCloseLandingPage:(JgAdsBannerAdView *)jgadsBannerView{
    [self.bridge bannerAdWillDismissFullScreenModal:self bannerView:jgadsBannerView];
}
@end
